package me.khrystal.rxnews.module.news.presenter;

import com.socks.library.KLog;

import java.util.List;

import me.khrystal.rxnews.base.BasePresenterImpl;
import me.khrystal.rxnews.bean.NeteastNewsSummary;
import me.khrystal.rxnews.common.DataLoadType;
import me.khrystal.rxnews.module.news.model.INewsListInteractor;
import me.khrystal.rxnews.module.news.model.INewsListInteractorImpl;
import me.khrystal.rxnews.module.news.view.INewsListView;
import me.khrystal.rxnews.module.news.view.INewsView;

/**
 * usage:
 * author: kHRYSTAL
 * create time: 16/4/22
 * update time:
 * email: 723526676@qq.com
 */
public class INewsListPresenterImpl extends BasePresenterImpl<INewsListView,List<NeteastNewsSummary>>
    implements INewsListPresenter{

    private INewsListInteractor<List<NeteastNewsSummary>> mNewsListInteractor;
    private String mNewsType;
    private String mNewsId;
    private int mStartPage;

    private boolean mIsRefresh = true;
    private boolean mHasInit;

    public INewsListPresenterImpl(INewsListView newsListView,String newsId,String newsType) {
        super(newsListView);
        mNewsListInteractor = new INewsListInteractorImpl();
        mSubscription = mNewsListInteractor.requestNewsList(this,newsType,newsId,mStartPage);
        mNewsType = newsType;
        mNewsId = newsId;
    }

    @Override
    public void beforeRequest() {
        if (!mHasInit){
            mView.showProgress();
        }
    }

    @Override
    public void requestError(String msg) {
        super.requestError(msg);
        mView.updateNewsList(null,mIsRefresh ? DataLoadType.TYPE_REFRESH_FAIL
            : DataLoadType.TYPE_LOAD_MORE_FAIL);
    }

    @Override
    public void requestSuccess(List<NeteastNewsSummary> data) {
        KLog.e("请求成功");
        mHasInit = true;
        if (data != null){
            mStartPage += 20;
        }
        mView.updateNewsList(data,
                mIsRefresh ? DataLoadType.TYPE_REFRESH_SUCCESS : DataLoadType.TYPE_LOAD_MORE_FAIL);
    }


    @Override
    public void refreshData() {
        mStartPage = 0;
        mIsRefresh = true;
        mSubscription = mNewsListInteractor.requestNewsList(this,mNewsType,mNewsId,mStartPage);
    }

    @Override
    public void loadMoreData() {
        mIsRefresh = false;
        mSubscription = mNewsListInteractor.requestNewsList(this,mNewsType,mNewsId,mStartPage);
    }
}
